package com.aier.admin_new.service.impl;

import java.util.List;
import org.springframework.stereotype.Component;
import junit.framework.Assert;

import com.aier.admin_new.service.IChannelClient;
import com.aier.common.common.SystemConfig;
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.BindInfo;
import com.baidu.yun.channel.model.ChannelMessage;
import com.baidu.yun.channel.model.FetchMessageRequest;
import com.baidu.yun.channel.model.FetchMessageResponse;
import com.baidu.yun.channel.model.InitAppIoscertRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.channel.model.PushTagMessageRequest;
import com.baidu.yun.channel.model.PushTagMessageResponse;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.channel.model.QueryBindListRequest;
import com.baidu.yun.channel.model.QueryBindListResponse;
import com.baidu.yun.channel.model.VerifyBindRequest;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.google.gson.JsonObject;


@Component("channelClient")
public class ChannelClientImpl implements IChannelClient {


    
    public List<BindInfo> QueryBindList(String UserId)  {

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });
        List<BindInfo> bindInfos=null;
        try {
            // 4. 创建请求类对象
            QueryBindListRequest request = new QueryBindListRequest();
            
            request.setUserId(UserId);

            // 5. 调用queryBindList接口
            QueryBindListResponse response = channelClient
                    .queryBindList(request);

            // 6. 对返回的结果对象进行操作
           bindInfos = response.getBinds();
           
//            for (BindInfo bindInfo : bindInfos) {
//                long channelId = bindInfo.getChannelId();
//                String userId = bindInfo.getUserId();
//                int status = bindInfo.getBindStatus();
//                System.out.println("channel_id:" + channelId + ", user_id: "
//                        + userId + ", status: " + status);
//
//                String bindName = bindInfo.getBindName();
//                long bindTime = bindInfo.getBindTime();
//                String deviceId = bindInfo.getDeviceId();
//                int deviceType = bindInfo.getDeviceType();
//                long timestamp = bindInfo.getOnlineTimestamp();
//                long expire = bindInfo.getOnlineExpires();
//
//                System.out.println("bind_name:" + bindName + "\t"
//                        + "bind_time:" + bindTime);
//                System.out.println("device_type:" + deviceType + "\tdeviceId"
//                        + deviceId);
//                System.out.println(String.format("timestamp: %d, expire: %d",
//                        timestamp, expire));
//
//            }

        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
        return bindInfos;
    }

    
    public void PushUnicastMessage(Long channelId,String UserId,String title,String description,Integer type,Long uid) {

        /*
         * @brief 推送单播消息(消息类型为透传，由开发方应用自己来解析消息内容)
         * message_type = 0 (默认为0)
         */
        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            // 4. 创建请求类对象
            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
            request.setDeviceType(3);
            request.setChannelId(channelId);
            request.setUserId(UserId);
            String[] str  = description.split("#");
            JsonObject jsonOject = new JsonObject();
            jsonOject.addProperty("title", title);
            jsonOject.addProperty("description", str[0]);
            jsonOject.addProperty("type", type);
            jsonOject.addProperty("uid", uid);
            if(str.length>=2){
            	jsonOject.addProperty("info", str[1]);
            }
            request.setMessage(jsonOject.toString());

            // 5. 调用pushMessage接口
            PushUnicastMessageResponse response = channelClient
                    .pushUnicastMessage(request);
            Assert.assertEquals(1, response.getSuccessAmount());
        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

    
    public void PushUnicastNotification(String DeviceType, Long channelId,String UserId,String title,String description,Integer type,Long uid) {

        /*
         * @brief 向Android端设备推送单播消息
         * message_type = 1
         * device_type = 3
         */

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
        	//-------------------------Android-------------------------//
        	if(DeviceType=="an"){
            // 4. 创建请求类对象
            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
            request.setDeviceType(3);
            request.setChannelId(channelId);
            request.setUserId(UserId);

            request.setMessageType(0);
            JsonObject jsonOject = new JsonObject();
            jsonOject.addProperty("title", title);
            jsonOject.addProperty("description", description);
            jsonOject.addProperty("type", type);
            jsonOject.addProperty("uid", uid);
            jsonOject.addProperty("info", "");
            request.setMessage(jsonOject.toString());

            // 5. 调用pushMessage接口
            PushUnicastMessageResponse response = channelClient
                    .pushUnicastMessage(request);
            Assert.assertEquals(1, response.getSuccessAmount());
            
        	}
        	else if(DeviceType=="ios"){
            //---------------IOS-----------------------------------------//
            // 4. 创建请求类对象
            PushUnicastMessageRequest requestIOS = new PushUnicastMessageRequest();
            requestIOS.setDeviceType(4);
            // request.setChannelId(4035698885061886729L);
            requestIOS.setUserId(UserId);

            requestIOS.setMessageType(0);
            JsonObject jsonOject = new JsonObject();
            jsonOject.addProperty("title", title);
            jsonOject.addProperty("description", description);
            jsonOject.addProperty("type", type);
            jsonOject.addProperty("uid", uid);
            jsonOject.addProperty("info", "");
            requestIOS.setMessage(jsonOject.toString());

            // 5. 调用pushMessage接口
            	PushUnicastMessageResponse responseIOS = channelClient
                    .pushUnicastMessage(requestIOS);
            	 Assert.assertEquals(1, responseIOS.getSuccessAmount());
        	}
        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

    
    public void PushUnicastIosNotification(String UserId,String title,String description,Integer type,Long uid) {

        /*
         * @brief 推送单播消息(IOS APNS)
         * message_type = 1 
         * device_type = 4
         */

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
            request.setDeviceType(4);
            // request.setChannelId(4035698885061886729L);
            request.setUserId(UserId);

            request.setMessageType(1);
            JsonObject jsonOject = new JsonObject();
            jsonOject.addProperty("title", title);
            jsonOject.addProperty("description", description);
            jsonOject.addProperty("type", type);
            jsonOject.addProperty("uid", uid);
            request.setMessage(jsonOject.toString());

            // 5. 调用pushMessage接口
            PushUnicastMessageResponse response = channelClient
                    .pushUnicastMessage(request);

            Assert.assertEquals(1, response.getSuccessAmount());

        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

    
    public void PushTagMessage(String tagName,String title,String description,Integer type,Long uid) {

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            PushTagMessageRequest request = new PushTagMessageRequest();
            request.setMessageType(0);
            request.setDeviceType(3);
            request.setTagName(tagName);
            
            String[] str  = description.split("#");
            JsonObject jsonOject = new JsonObject();
            jsonOject.addProperty("title", title);
            jsonOject.addProperty("description", str[0]);
            jsonOject.addProperty("type", type);
            jsonOject.addProperty("uid", uid); 
            if(str.length>=2){
            	jsonOject.addProperty("info", str[1]);
            }
            request.setMessage(jsonOject.toString());

            // 5. 调用pushMessage接口
            PushTagMessageResponse response = channelClient
                    .pushTagMessage(request);
            if (response.getSuccessAmount() == 1) {
                // TODO
            }
            System.out.println("tagName： "+tagName);
        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

    
    public void PushBroadcastMessage(String title,String description,Integer type,Long uid) {

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
            request.setMessageType(0);
            request.setDeviceType(3);
            JsonObject jsonOject = new JsonObject();
            jsonOject.addProperty("title", title);
            jsonOject.addProperty("description", description);
            jsonOject.addProperty("type", type);
            jsonOject.addProperty("uid", uid);        
            request.setMessage(jsonOject.toString());

            // 5. 调用pushMessage接口
            PushBroadcastMessageResponse response = channelClient
                    .pushBroadcastMessage(request);
            if (response.getSuccessAmount() == 1) {
                // TODO
            }

        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
    }

    
    public void BindVerify() {

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            VerifyBindRequest request = new VerifyBindRequest();
            request.setChannelId(4035698885061886729L);
            request.setUserId("1144280722819924199");

            // 5. 调用 verifyBind 接口
            channelClient.verifyBind(request);

        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

    // ----------------------------------------------------------------------------
    
    public void FetchMessage(String userId) {

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            FetchMessageRequest request = new FetchMessageRequest();
            request.setUserId(userId);

            // 5. 调用 fetchMessage 接口
            FetchMessageResponse resp = channelClient.fetchMessage(request);
            List<ChannelMessage> messages = resp.getMessages();
            for (ChannelMessage msg : messages) {
                System.out.println(msg.getData());
            }

        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

    
    public void InitIosCert() {

        // 1. 设置developer平台的ApiKey/SecretKey
        String apiKey = SystemConfig.apiKey;;
        String secretKey = SystemConfig.secretKey;
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            InitAppIoscertRequest request = new InitAppIoscertRequest();
            request.setName("name");
            request.setDescription("description");

            request.setDevCert("");
            request.setReleaseCert("");

            // 5. 调用 initAppIoscert 接口
            channelClient.initAppIoscert(request);

        } catch (ChannelClientException e) {
            e.printStackTrace();
            // 处理客户端错误异常
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }

    }

}
